package com.snail.mobile.sim.oss.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;

import javax.imageio.ImageIO;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts.upload.FormFile;

import com.snail.mobile.sim.oss.util.FileUtil;
import com.snail.mobile.sim.oss.util.PicUtil;

public class PicUtil {

	public static boolean isPicture(String pInput, String pImgeFlag) throws Exception {
		// 文件名称为空的场合
		if (StringUtils.isEmpty(pInput)) {
			// 返回不和合法
			return false;
		}
		// 获得文件后缀名
		String tmpName = pInput.substring(pInput.lastIndexOf(".") + 1, pInput.length());
		// 声明图片后缀名数组
		String imgeArray[][] = { { "bmp", "0" }, { "dib", "1" }, { "gif", "2" }, { "jfif", "3" }, { "jpe", "4" },
				{ "jpeg", "5" }, { "jpg", "6" }, { "png", "7" }, { "tif", "8" }, { "tiff", "9" }, { "ico", "10" } };
		// 遍历名称数组
		for (int i = 0; i < imgeArray.length; i++) {
			// 判断单个类型文件的场合
			if (StringUtils.isNotEmpty(pImgeFlag) && imgeArray[i][0].equals(tmpName.toLowerCase())
					&& imgeArray[i][1].equals(pImgeFlag)) {
				return true;
			}
			// 判断符合全部类型的场合
			if (StringUtils.isEmpty(pImgeFlag) && imgeArray[i][0].equals(tmpName.toLowerCase())) {
				return true;
			}
		}
		return false;
	}

	public static String uploadpic(FormFile ff, String picPath,String filerelative) throws Exception {
		InputStream in = ff.getInputStream();
		String filepath = picPath + ff.getFileName();
		File file = new File(picPath);
		if (!file.exists() && !file.isDirectory()) {
			file.mkdirs();
		}

		

		OutputStream out = new FileOutputStream(filepath);
		int bytesRead = 0;
		byte[] buffer = new byte[8192];
		while ((bytesRead = in.read(buffer, 0, 8192)) != -1) {
			out.write(buffer, 0, bytesRead);
		}
		out.close();
		in.close();
		return filerelative;
	}

	public static String uploadpic(FormFile ff, String picPath, String fileName, String filerelative) throws Exception {
		InputStream in = ff.getInputStream();

		String filepath = picPath + fileName;
		File file = new File(picPath);
		if (!file.exists() && !file.isDirectory()) {
			file.mkdir();
		}

		OutputStream out = new FileOutputStream(filepath);
		int bytesRead = 0;
		byte[] buffer = new byte[8192];
		while ((bytesRead = in.read(buffer, 0, 8192)) != -1) {
			out.write(buffer, 0, bytesRead);
		}
		out.close();
		in.close();
		return filerelative;
	}

	public static boolean validateImageFileType(FormFile formFile) {
		if (formFile != null && formFile.getFileSize() > 0) {

			String ext = formFile.getFileName().substring(formFile.getFileName().lastIndexOf('.') + 1).toLowerCase();

			List<String> arrowType = Arrays.asList("image/bmp", "image/png", "image/gif", "image/jpg", "image/jpeg",
					"image/pjpeg");

			List<String> arrowExtension = Arrays.asList("gif", "jpg", "bmp", "png");
			return arrowType.contains(formFile.getContentType().toLowerCase()) && arrowExtension.contains(ext);
		}
		return false;
	}

	public static String createPic(String path, String nid, FormFile ff, String type) throws Exception {
		InputStream inputStream = ff.getInputStream();
		BufferedImage bi = ImageIO.read(inputStream);
		String filewidth = String.valueOf(bi.getWidth());
		String fileheight = String.valueOf(bi.getHeight());
		String totalname = ff.getFileName();

		String url = "";
		String extend = totalname.substring(totalname.lastIndexOf("."));

		String descPath = path + File.separator + type + File.separator + nid + File.separator;

		String fileName = nid + "_" + filewidth + "x" + fileheight + extend;

		String filerelative = File.separator + type + File.separator + nid + File.separator + fileName;

		if (FileUtil.createDir(descPath)) {
			url = PicUtil.uploadpic(ff, descPath, fileName, filerelative);
		}

		return url;

	}
	public static String createPic(String path,  FormFile ff, String type) throws Exception {
		
		InputStream inputStream = ff.getInputStream();
		BufferedImage bi = ImageIO.read(inputStream);
	
		String totalname = ff.getFileName();

		String url = "";
		

		String descPath = path + File.separator + type + File.separator ;

		String fileName = totalname;

		String filerelative = File.separator + type + File.separator + fileName;

		if (FileUtil.createDir(descPath)) {
			url = PicUtil.uploadpic(ff, descPath, fileName, filerelative);
		}

		return url;

	}

}
